package org.toolbook.controller;

import java.util.Date;

import org.toolbook.interceptor.LoginInterceptor;
import org.toolbook.model.Code;
import org.toolbook.model.User;
import org.toolbook.util.FileUtils;
import org.toolbook.util.LogUtil;
import org.toolbook.util.PropUtil;
import org.toolbook.validator.AddCodeValidator;
import org.toolbook.validator.UpdateCodeValidator;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
/**
 * 代码类控制器
 * @author meigang
 * @date 2015-06-23 13:28
 *
 */
@Before(LoginInterceptor.class)
public class CodeController extends Controller{
	private static String tablePre = PropUtil.getProperty("tablePre");
	/**
	 * 代码首页
	 */
	public void index(){
		//查询code分页
		Integer pageNumber = getParaToInt("page");
		if(pageNumber == null){
			pageNumber = 1;
		}
		
		Page<Code> codePage = Code.dao.paginate(pageNumber, 
				Integer.parseInt(PropUtil.getProperty("CODE_LIST_PAGE_SIZE")),
				"select *", "from "+tablePre+"code order by isTop desc,publishTime desc");
		setAttr("codePage", codePage);
		render("/WEB-INF/pages/fc/code/list.jsp");
	}
	/**
	 * 跳转到添加代码界面
	 */
	public void toAdd(){
		LogUtil.log.info("跳转到add code界面");
		render("/WEB-INF/pages/fc/code/add.jsp");
	}
	/**
	 * 添加代码
	 */
	@Before(AddCodeValidator.class)
	public void add(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		Code code = new Code().set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("tags", getPara("tags"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("publishTime", new Date())
				.set("updateTime", new Date())
				.set("readCount", 0)
				.set("isTop", 0)
				.set("isShare", 0)
				.set("checkState", 0)
				.set("codeType", getPara("codeType"))
				.set("userId", u.get("id"));
		code.save();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]新增代码成功");
		index();
	}
	/**
	 * 显示代码页
	 */
	public void show(){
		int id = getParaToInt(0);
		Code code = Code.dao.findById(id);
		//更新阅读次数
		code.set("readCount", code.getInt("readCount")+1).update();
		setAttr("code", code);
		LogUtil.log.info("查看code[id:"+id+"]");
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	/**
	 * 删除
	 */
	public void delete(){
		int id = getParaToInt(0);
		Code code = Code.dao.findById(id);
		FileUtils.deleteFile(code.getStr("lookPhoto"));
		code.delete();
		LogUtil.log.info("删除code[id:"+id+"]");
		index();
	}
	/**
	 * 修改
	 */
	public void toUpdate(){
		int id = getParaToInt(0);
		Code code = Code.dao.findById(id);
		setAttr("code", code);
		LogUtil.log.info("去修改界面code[id:"+id+"]");
		render("/WEB-INF/pages/fc/code/update.jsp");
	}
	/**
	 * 修改代码
	 */
	@Before(UpdateCodeValidator.class)
	public void update(){
		User u = getSessionAttr(PropUtil.getProperty("SESSION_LOGIN_KEY"));
		Code code = Code.dao.findById(getParaToInt("id"));
		code.set("title", getPara("title"))
				.set("lookPhoto", getPara("lookPhotoFilePath"))
				.set("tags", getPara("tags"))
				.set("note", getPara("note"))
				.set("content", getPara("content"))
				.set("updateTime", new Date())
				.set("codeType", getPara("codeType"));
		code.update();
		LogUtil.log.info("用户[ "+ u.get("username")+" ]修改code[id:"+code.getInt("id")+"]代码成功");
		setAttr("code", code);
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	/**
	 * 置顶
	 */
	public void top(){
		Code code = Code.dao.findById(getParaToInt(0)).set("isTop", 1);
		code.update();
		LogUtil.log.info("置顶code[id:"+getParaToInt(0)+"]代码成功");
		setAttr("code", code);
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	/**
	 * 取消置顶
	 */
	public void notop(){
		Code code = Code.dao.findById(getParaToInt(0)).set("isTop", 0);
		code.update();
		LogUtil.log.info("取消置顶code[id:"+getParaToInt(0)+"]代码成功");
		setAttr("code", code);
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	/**
	 * 分享
	 */
	public void share(){
		Code code = Code.dao.findById(getParaToInt(0)).set("isShare", 1);
		code.update();
		LogUtil.log.info("分享code[id:"+getParaToInt(0)+"]代码成功");
		setAttr("code", code);
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	/**
	 * 取消分享
	 */
	public void noshare(){
		Code code = Code.dao.findById(getParaToInt(0)).set("isShare", 0);
		code.update();
		LogUtil.log.info("取消分享code[id:"+getParaToInt(0)+"]代码成功");
		setAttr("code", code);
		render("/WEB-INF/pages/fc/code/show.jsp");
	}
	
}
